<?php
/**
 * @version SVN: $Id$
 * @package    fmcSeminaire
 * @subpackage Models
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Ludovic Launer {@link }
 * @author     Created on 10-Nov-2010
 * @license    GNU/GPL
 */

//-- No direct access
defined('_JEXEC') or die('=;)');

jimport('joomla.application.component.model');

/**
 * fmcSeminaire Model
 *
 * @package    fmcSeminaire
 * @subpackage Models
 */
class fmcSeminaireListModelfmcSeminaire extends JModel
{
    /**
     * Constructor that retrieves the ID from the request.
     *
     * @access	public
     * @return	void
     */
    function __construct()
    {
        parent::__construct();

        $array = JRequest::getVar('cid', 0, '', 'array');
        $this->setId((int)$array[0]);
    }//function

    /**
     * Method to set the fmcSeminaire identifier.
     *
     * @access	public
     * @param	int $id fmcSeminaire identifier
     * @return	void
     */
    function setId($id)
    {
        // Set id and wipe data
        $this->_id = $id;
        $this->_data = null;
    }//function


    /**
     * Method to get a record.
     *
     * @return object with data
     */
    function &getData()
    {
        // Load the data
        if(empty($this->_data))
        {
            $query = 'SELECT * FROM #__fmcseminaire'
            . ' WHERE seminaireId = '.(int)$this->_id;
            $this->_db->setQuery($query);
            $this->_data = $this->_db->loadObject();
        }

        if( ! $this->_data)
        {
            $this->_data = $this->getTable();
        }

        return $this->_data;
    }//function

    /**
     * Method to store a record
     *
     * @access	public
     * @return	boolean	True on success
     */
    function store()
    {
        $row =& $this->getTable();

        $data = JRequest::get('post');
        
        // --- Add extra info ---
        // Ludo: I'm sure there must be a better way of doing this..but don't know how..:-(
        $data['content_id'] = $data['selected_article']['content_id'];
        if ($data['content_id']==0) $data['content_id']=NULL;

        //-- Bind the form fields to the hello table
        if( ! $row->bind($data))
        {
            $this->setError($this->_db->getError());
            return false;
        }

        //-- Make sure the record is valid
        if( ! $row->check())
        {
            $this->setError($this->_db->getError());
            return false;
        }

        //-- Store the table to the database
        if( ! $row->store())
        {
            $this->setError($row->getError());
            return false;
        }

        return true;
    }//function

    /**
     * Method to delete record(s).
     *
     * @access	public
     * @return	boolean	True on success
     */
    function delete()
    {
        $cids = JRequest::getVar('cid', array(0), 'post', 'array');

        $row =& $this->getTable();

        if (count($cids))
        {
            foreach($cids as $cid)
            {
                if( ! $row->delete($cid))
                {
                    $this->setError($row->getError());
                    return false;
                }
            }//foreach
        }

        return true;
    }//function

}//class
